<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right" v-show="condition">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
          <el-divider v-show="condition"></el-divider>
        </el-breadcrumb>

        <!-- 搜索 -->
        <el-form :inline="true" ref="form">
          <div class="filter-container">
            <el-form-item label="标签名称 ">
              <el-input
                @keyup.enter="toseach"
                v-model="form.tagName"
                style="width: 180px"
                placeholder="请输入"
                class="filter-item"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态 ">
              <el-select v-model="form.state" class="filter-item" style="width: 180px">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button size="small" @click="clearseach">清除</el-button>
            </el-form-item>

            <el-form-item>
              <el-button size="small" type="primary" @click="toseach">搜索</el-button>
            </el-form-item>
            <div class="box">
              <el-button
                v-show="condition"
                size="small"
                style="margin-left: 10px"
                @click="goback"
                type="text"
                icon="el-icon-edit"
                >返回学科</el-button
              >
              <el-button
                class="filter-item fr"
                size="small"
                style="margin-left: 10px"
                type="success"
                icon="el-icon-edit"
                @click="addDialog"
                >新增用户</el-button
              >
            </div>
          </div>
        </el-form>
        <!-- end -->
        <!-- 数据记录 -->
        <el-alert
          v-if="alertText !== ''"
          :title="alertText"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- end -->
        <!-- 数据 -->
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序号" width="80"> </el-table-column>
          <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
          <el-table-column prop="tagName" label="标签名称"> </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column label="创建日期">
            <template v-slot="{ row }">
              {{ row.addDate | dtformat }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template v-slot="{ row }">
              <span v-if="row.state == '1'">已启用</span>
              <span v-if="row.state == '0'">已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button type="text" size="small" @click="hSwitchStatus(scope.row)">
                <span>{{ scope.row.state === 0 ? '启用' : '禁用' }}</span>
              </el-button>
              <el-button type="text" size="small" :disabled="scope.row.state === 1" @click="editdialog(scope.row.id)"
                >修改</el-button
              >
              <el-button type="text" size="small" :disabled="scope.row.state === 1" @click="deletedialog(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="form.page"
          :page-size="form.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <!-- end -->
        <!-- 新增标签弹层 -->
        <!-- <tags-add-vue :visible="dialogFormVisible" @close-dialog="closeDialog"/> -->

        <el-dialog :title="dialogTitle" :visible="dialogFormVisible" width="20%" @close="closeDialog">
          <el-form :model="dialogform" :rules="rules" ref="dialogform">
            <el-form-item label="所属学科" prop="subjectID">
              <el-select v-model="dialogform.subjectID" placeholder="请选择" style="width: 60%">
                <el-option
                  v-for="item in subjectList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="目录名称" prop="tagName">
              <el-input v-model="dialogform.tagName" style="width: 60%" placeholder="请输入目录名称"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <div style="text-align: right">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
          </div>
        </el-dialog>
        <!-- 新增弹层 -->
        <el-dialog :title="dialogTitle" :visible="dialogFormVisible1" width="20%" @close="closeDialog1">
          <el-form :model="dialogform" :rules="rules" ref="dialogform">
             <el-form-item label="目录名称" prop="tagName">
              <el-input v-model="dialogform.tagName" style="width: 60%" placeholder="请输入目录名称"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <div style="text-align: right">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
          </div>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, add, update, detail, remove, changeState } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
// import tagsAddVue from '../components/tags-add.vue'
export default {
  // components: {
  //   tagsAddVue
  // },
  data () {
    return {
      tableData: [],
      alertText: '',
      total: 0,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        tagName: '',
        state: null,
        page: 1,
        pagesize: 10
      },
      dialogform: {
        subjectID: '', // 所属学科
        tagName: '' // 标签名称
      },
      rules: {
        subjectID: [{ required: true, message: '所属学科不能为空', trigger: 'blur' }],
        tagName: [{ required: true, message: '目录名称不能为空', trigger: 'blur' }]
      },
      dialogTitle: '',
      subjectList: [],
      condition: false,
      addDia: false
    }
  },
  mounted () {
    if (this.$route.query.id) {
      this.conditionFn()
    } else {
      this.getlist()
    }
  },
  watch: {
    $route: {
      handler () {
        this.getlist()
        this.condition = false
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    async getlist () {
      const res = await list(this.form)
      this.tableData = res.data.items
      const counts = res.data.counts
      this.total = counts
      this.alertText = `数据一共 ${counts} 条`
    },
    // 面包屑
    async conditionFn () {
      if (this.$route.query.id) {
        this.condition = true
        const res = await list({ subjectID: this.$route.query.id })

        console.log('面包屑', res)
        this.tableData = res.data.items
        const counts = res.data.counts
        this.total = counts
        this.alertText = `数据一共 ${counts} 条`
      } else {
        this.condition = false
      }
    },
    // 分页
    handleSizeChange (val) {
      this.form.pagesize = val
      this.from.page = 1
      this.getlist()
    },
    handleCurrentChange (val) {
      this.form.page = val
      this.getlist()
    },
    // 新增按钮
    addDialog () {
      this.dialogTitle = '新增目录'
      if (this.$route.query.id) {
        this.dialogFormVisible1 = true
      } else {
        this.dialogFormVisible = true
      }

      this.getsubjectList()
    },
    closeDialog () {
      this.dialogFormVisible = false
    },
    closeDialog1 () {
      this.dialogFormVisible1 = false
    },
    // 获取所属学科列表
    async getsubjectList () {
      const res = await simple()
      this.subjectList = res.data
    },
    async doSwitch (row) {
      const data = {
        id: row.id,
        state: Number(!row.state)
      }
      await changeState(data)
      this.$message.success('操作成功!')
      this.getlist()
    },
    // 启用、禁用
    hSwitchStatus (row) {
      this.doSwitch(row)
    },
    // 提交表单添加
    submitForm () {
      this.$refs.dialogform.validate(async valid => {
        if (valid) {
          if (this.dialogform.id) {
            await update(this.dialogform)
          } else {
            await add(this.dialogform)
          }
          this.$message.success(`${this.dialogform.id ? '编辑' : '新增'}成功`)
          this.$refs.dialogform.resetFields()
          this.dialogFormVisible = false
          this.getlist()
        }
      })
    },
    // 编辑按钮
    async editdialog (id) {
      this.dialogTitle = '编辑目录'
      this.dialogFormVisible = true
      const res = await detail({ id: id })
      this.dialogform = res.data
    },
    // 删除按钮
    async deletedialog (id) {
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async () => {
        await remove({ id })
        this.$message.success('删除操作成功')
        this.getlist()
      })
    },
    // 清除按钮
    clearseach () {
      this.form = {
        tagName: '',
        state: null
      }
    },
    // 搜索按钮
    toseach () {
      this.form.page = 1
      this.getlist()
    },
    goback () {
      this.$router.push('/subjects/list')
    }
  }
}
</script>

<style scoped lang="scss">
.el-pagination {
  margin-top: 10px;
  float: right;
}
.box {
  float: right;
}
.filter-item {
  margin-right: 30px;
}
.el-table {
  margin-top: 10px;
}
.el-breadcrumb {
  margin-left: 10px;
  font-size: 14px;
}
</style>
